//
//  HWjiluYaoQingliuzhangViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2020/11/26.
//  Copyright © 2020 Chendaobin. All rights reserved.
//

#import "HWjiluYaoQingliuzhangViewController.h"
#import "HWyaoqingjiluTableViewCell.h"
#import "HWzhongjianModel.h"

@interface HWjiluYaoQingliuzhangViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(strong,nonatomic)UITableView *mainaTableVie;

@property(strong,nonatomic)  NSString *p;
@property(strong,nonatomic)  NSString *max_p;
@property(strong,nonatomic)  NSMutableArray *dataMuArr;


@end

@implementation HWjiluYaoQingliuzhangViewController

-(UITableView *)mainaTableVie {

    if (_mainaTableVie == nil
        ) {

         _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(12, 0, kScreenW-24,kScreenH) style:UITableViewStylePlain];
         _mainaTableVie.delegate = self;
         _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        

    }
    return   _mainaTableVie ;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
}


- (void)setupUI {
    
    self.navigationItem.title = @"中奖记录";
    self.navigationItem.title =  [HWjianzhiduiObject keylocalization:@"中奖记录"];
    
    self.view.backgroundColor = RGB(247, 247, 247);
    self.mainaTableVie.backgroundColor = RGB(247, 247, 247);
    [self.view addSubview:self.mainaTableVie];
    [self addrefreshControl];
    
}


#pragma  mark ---- 添加刷新

- (void)addrefreshControl {
    
    self.mainaTableVie.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    
   [self.mainaTableVie.mj_header beginRefreshing];
    
    self.mainaTableVie.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
}

// 下拉刷新
- (void)underRefresh {
    
   
    
    self.p = @"1";
     
    NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    
    NSString *typeStr =  [NSString stringWithFormat:@"%@",@"0"];
    
    NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
    
    NSString *singMD5 = [WMUtil md5HexDigest:singStr];

    self.dataMuArr = [[NSMutableArray alloc]init];
    NSDictionary *parametes = @{
                                       @"p" :self.p,
                                       @"ios" : @"1",
                                       @"ct" :  @"app",
                                       @"ac" : @"choujiang_log",
                                       @"uid" :uidStr,
                                       @"t" : tStr,
                                       @"member_id":[HWLocallyStoredData getCode],
                                       @"sign" : singMD5
                                       };
       
       [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
           
           // [SVProgressHUD dismiss];
           [self.mainaTableVie.mj_header endRefreshing];
           [self.mainaTableVie.mj_footer endRefreshing];
           
           
           NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
           
           if ([codeStr intValue] == 1) {
               
                  self.dataMuArr = [HWzhongjianModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
               
               
               
               self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
               self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
               
               [self.mainaTableVie reloadData];
               
    
           }   else {
               
               
               
               [MBProgressHUD showError:response[@"message"]];
           }
        
           
           
       } failure:^(NSString *error) {
           
           
             [MBProgressHUD showError:error];
    
           [self.mainaTableVie.mj_header endRefreshing];
           [self.mainaTableVie.mj_footer endRefreshing];
           
           
       }];
    
    
}

// 上拉加载

- (void)onRefresh {
    
    
   
    int p = [self.p intValue];
                   
    int max_p = [self.max_p intValue];
                   
     if (p > max_p) {
        [MBProgressHUD showError:@"没有更多数据，请待会在上拉加载"];
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        return ;
         
        
      }
                   

      NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
      NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
     
      NSString *typeStr =  [NSString stringWithFormat:@"%@",@"0"];
     
      NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
     
     NSString *singMD5 = [WMUtil md5HexDigest:singStr];

   NSDictionary *parametes = @{
                       @"p" :self.p,
                       @"ios" : @"1",
                       @"ct" :  @"app",
                       @"ac" : @"choujiang_log",
                       @"uid" :uidStr,
                      @"t" : tStr,
                       @"member_id":[HWLocallyStoredData getCode],
                      @"sign" : singMD5
      };
         
        [HWWebUtils do3GetWithParameters:parametes progress:nil success:^(id response) {
             
             // [SVProgressHUD dismiss];
             [self.mainaTableVie.mj_header endRefreshing];
             [self.mainaTableVie.mj_footer endRefreshing];
             
             
             NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
             
             if ([codeStr intValue] == 1) {
                 
                  NSMutableArray *mutableArr = [HWzhongjianModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];


                 for (int i = 0; i < mutableArr.count; i++) {
                     [self.dataMuArr addObject:mutableArr[i]];

                 }

                 self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                 self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                 [self.mainaTableVie reloadData];
                 
      
             }
          
             
             
         } failure:^(NSString *error) {
             
             [self.mainaTableVie.mj_header endRefreshing];
             [self.mainaTableVie.mj_footer endRefreshing];
            
         }];
      
    
}










- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    

//     return  10;
    
    return self.dataMuArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cellID";
    
    HWyaoqingjiluTableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[HWyaoqingjiluTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        
    }
    cell.selectionStyle   =  UITableViewCellSelectionStyleNone;
    
    

     HWzhongjianModel *model= self.dataMuArr[indexPath.row];
     cell.dataModel = model;

    return  cell;
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    return  36;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    
    
    return  36;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW-24, 36)];
    view.backgroundColor = RGB(233, 241, 255);
    view.backgroundColor = [UIColor whiteColor];
  //    NSArray *arr = @[@"时间",@"记录",@"金币"];
    
    NSArray *arr = @[[HWjianzhiduiObject keylocalization:@"时间"],[HWjianzhiduiObject keylocalization:@"记录"],[HWjianzhiduiObject keylocalization:@"金币"]];
    for (int i = 0; i < 3; i++) {
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((kScreenW-24)/3*i, 0, (kScreenW-24)/3, 36)];
        label.text = arr[i];
        label.font = [UIFont systemFontOfSize:13];
        label.textColor = RGB(51, 51, 51);
        label.textAlignment = NSTextAlignmentCenter;
        
        label.backgroundColor = RGB(233, 241, 255);
        [view addSubview:label];
        
    }
    
    for (int i = 0 ; i < 2 ; i++) {
        UIView *shuxiangView = [[UIView alloc]initWithFrame:CGRectMake((kScreenW-24)/3*i, 0, 1, 36)];
        shuxiangView.backgroundColor = RGB(232, 232, 242);
        [view addSubview:shuxiangView];
    }
    
    return  view;
    
}


@end
